#include "PreCompiled.h"
#include "View3DInventor.h"
#include "view3DInventorViewer.h"
#include "Document_Gui.h"
#include "MainWindow.h"

TYPESYSTEM_SOURCE_ABSTRACT(View3DInventor,ViewWindow);
View3DInventor::View3DInventor(Document_Gui* pcDoc,QWidget* parent, Qt::WFlags wflags)
:ViewWindow(pcDoc,parent,wflags),_viewer(NULL)
{
 	_viewer = new view3DInventorViewer(this);

  	setMouseTracking(true);

  	setAcceptDrops(true);
 
    setCentralWidget(_viewer);
     
	_viewer->setDocument(pcDoc);
}

View3DInventor::~View3DInventor(void)
{
	QWidget* foc = qApp->focusWidget();
	if (foc) {
		QWidget* par = foc->parentWidget();
		while (par) {
			if (par == this) {
				foc->setFocusProxy(0);
				foc->clearFocus();
				break;
			}
			par = par->parentWidget();
		}
	}

	delete _viewer;


}
void View3DInventor::dropEvent(QDropEvent *event)
{
	const QMimeData* mimeData = event->mimeData();

	if (mimeData->hasFormat(QLatin1String("text/x-action-items"))) {

		QByteArray itemData = mimeData->data(QLatin1String("text/x-action-items"));

		QDataStream dataStream(&itemData, QIODevice::ReadOnly);

		int ctActions; dataStream >> ctActions;

		for (int i=0; i<ctActions; i++) {

			QString action;

			dataStream >> action;

			std::string fname = (const char*)action.toAscii();

		}
	}


	ViewWindow::dropEvent(event);
}
 void View3DInventor::dragEnterEvent(QDragEnterEvent *e)
 {
	 e->accept();
 }
void View3DInventor::mouseDoubleClickEvent(QMouseEvent * e)
{

}
bool View3DInventor::eventFilter(QObject *o, QEvent *e)
{
    return QMainWindow::eventFilter(o, e);
}
void View3DInventor::setCurrentViewMode(ViewMode newmode)
{
	ViewMode oldmode = ViewWindow::currentViewMode();
	if(newmode == oldmode)
		return;

	ViewWindow::setCurrentViewMode(newmode);

	if(oldmode == Child)
	{
 		QList<QAction*>acts = getMainWindow()->findChildren<QAction*>();
 		this->addActions(acts);
 		_viewer->setFocusProxy(this);
 		qApp->installEventFilter(this);
	}else if(newmode == Child)
	{
		_viewer->setFocusProxy(0);
		qApp->removeEventFilter(this);
		QList<QAction*>acts = this->actions();
		for (QList<QAction*>::Iterator it = acts.begin(); it != acts.end(); ++it)
		{
			this->removeAction(*it);
		}
	}
}
bool View3DInventor::onMsg(const char* pMsg,const char** ppReturn)
{
    return true;
}
bool View3DInventor::onHasMsg(const char* pMsg)const
{
  return true;
}

#include "moc_View3DInventor.cpp"
